package com.snbc.Io;

import java.io.*;

/**
 * @Classname RemoveZhushi
 * @Description TODO
 * @Date 2019/9/27 22:54
 * @Created by Acer
 */
public class RemoveZhushi {

    public static void main(String[] args) {
//      创建文件对象，创建目录以及文件
        File f = new File("F:\\123444mmm\\test2.txt");
        if (!f.exists()) {
            try {
                f.getParentFile().mkdirs();
                f.createNewFile();
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
//      写入代码
        try (FileWriter fw = new FileWriter(f); PrintWriter pw = new PrintWriter(fw)) {
            pw.println("File f = new File(\"d:/LOLFolder/LOL.exe\");");
            pw.println("System.out.println(\"当前文件是：\" +f);");
            pw.println("//文件是否存在");
            pw.println("System.out.println(\"判断是否存在：\"+f.exists());");
            pw.println("//是否是文件夹");
            pw.println("System.out.println(\"判断是否是文件夹：\"+f.isDirectory());");
        } catch (Exception e) {
            e.getStackTrace();
        }
//      用缓存流读取内容

        try (FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr)) {
            System.out.println("原代码为:");
            String nextLine = "";
            while ((nextLine = br.readLine()) != null) {
                System.out.println(nextLine);
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
//      移除注释
         removeComments(f);
    }

    private static void removeComments(File javaFile) {

//      创建另外一个容器，用于盛放移除注释后的全新文件
        File newFile = new File("F:\\123444mmm\\test2-fuben");
        try (FileReader fr = new FileReader(javaFile);
             BufferedReader br = new BufferedReader(fr);
             FileWriter fw = new FileWriter(newFile);
             PrintWriter pw = new PrintWriter(fw)) {
            System.out.println("删除注释后:");
//          如果正确，则执行
            while (true) {
//              一段一段读取内容
                String line = br.readLine();
                if (null == line)
                    break;
//              如果不包含注释符号，则将内容写入
                if (!line.contains("//")) {
                    pw.println(line);
                    System.out.println(line);
                }
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
    }
}
